<template>
  <div class="search-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="header-content">
        <div class="search-title">
          <i class="el-icon-search"></i>
          搜索结果
        </div>
        <div class="search-keyword">
          搜索关键词：<span class="keyword-text">"{{ props.keyword }}"</span>
        </div>
        <div class="search-count" v-if="goods && goods.length > 0">
          共找到 <span class="count-number">{{ goods.length }}</span> 件商品
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <el-empty description="暂无相关商品" v-show="goods?.length == 0">
      <template #description>
        <div class="empty-content">
          <div class="empty-text">没有找到与 "{{ props.keyword }}" 相关的商品</div>
          <div class="empty-desc">建议您尝试其他关键词</div>
        </div>
      </template>
    </el-empty>

    <!-- 商品列表 -->
    <div v-if="goods && goods.length > 0" class="goods-container">
      <div class="goods-grid">
        <div v-for="item in goods" :key="item.id" class="goods-card" @click="goDetail(item)">
          <!-- 商品图片 -->
          <div class="goods-image">
            <div class="image-container">
              <el-image :src="item.image" fit="contain" class="product-image">
                <template #error>
                  <div class="image-error">
                    <i class="el-icon-picture-outline"></i>
                    <span>图片加载失败</span>
                  </div>
                </template>
                <template #placeholder>
                  <div class="image-placeholder">
                    <i class="el-icon-loading"></i>
                    <span>加载中...</span>
                  </div>
                </template>
              </el-image>
            </div>
            <!-- 商品状态标签 -->
            <div class="goods-tag" :class="item.status == 1 ? 'on-sale' : 'off-shelf'">
              {{ item.status == 1 ? '热卖中' : '已下架' }}
            </div>
            <!-- 品牌标签 -->
            <div class="brand-tag">{{ item.brand }}</div>
            <!-- 图片遮罩层 -->
            <div class="image-overlay">
              <div class="overlay-content">
                <i class="el-icon-view"></i>
                <span>查看详情</span>
              </div>
            </div>
          </div>

          <!-- 商品信息 -->
          <div class="goods-info">
            <div class="goods-name">{{ item.name }}</div>
            <div class="goods-desc">{{ item.description }}</div>
            
            <!-- 价格信息 -->
            <div class="price-section">
              <div class="current-price">
                <span class="price-symbol">¥</span>
                <span class="price-number">{{ item.discounted_price }}</span>
              </div>
              <div class="original-price" v-if="item.price > item.discounted_price">
                ¥{{ item.price }}
              </div>
              <div class="save-tag" v-if="item.price > item.discounted_price">
                省¥{{ (item.price - item.discounted_price).toFixed(2) }}
              </div>
            </div>

            <!-- 商品特性 -->
            <div class="goods-features">
              <span class="feature-item">官方正品</span>
              <span class="feature-item">全国联保</span>
              <span class="feature-item">急速发货</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 加载更多 -->
      <div v-show="hasMore" class="load-more" @click="loadGoods">
        <el-button type="primary" plain :loading="loadingMore" class="load-btn">
          {{ loadingMore ? '加载中...' : '加载更多' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { RequestPath, startRequest, GoodsItemData, GoodsResponseData } from '../base/RequestWork';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const props = defineProps(['keyword'])
// 路由管理对象
let router = useRouter()
// 商品数据
let goods = ref()
// 分页参数
let offset = 0
const limit = 4
// 是否有更多数据
let hasMore = ref(true)
// 加载状态
let loadingMore = ref(false)

// 页面挂载时加载数据
onMounted(()=>{
    loadGoods()
}) 

// 请求商品数据
function loadGoods() {
  loadingMore.value = true
  // 调用搜索接口
  startRequest(RequestPath.goodsListSearch, 'get', {'keyword': props.keyword, offset, limit}).then((response)=>{
      let data = response.data as GoodsResponseData
      let datas:GoodsItemData[] = goods.value ?? []
      // 判断返回的数据量是否小于limit，小于则表明没有更多数据了
      hasMore.value = (data.datas ?? []).length >= limit
      // 将返回的数据追加到数据源中
      datas.push(...(data.datas ?? []))
      goods.value = datas
      // 对offset进行设置
      offset = datas.length
  }).catch((error)=>{
      ElMessage.error({
          message: error.response.data.error
      })
  }).finally(() => {
    loadingMore.value = false
  })
}

// 跳转到商品详情页
function goDetail(item: GoodsItemData) {
    router.push({
        name: 'goodsDetail',
        params: {id: item.id}
    })
}
</script>

<style scoped>
.search-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: #fff;
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 30px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

.search-title {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.search-title i {
  margin-right: 10px;
  color: #409eff;
  font-size: 28px;
}

.search-keyword {
  font-size: 16px;
  color: #666;
}

.keyword-text {
  color: #ff4444;
  font-weight: 500;
}

.search-count {
  font-size: 14px;
  color: #999;
}

.count-number {
  color: #ff4444;
  font-weight: 500;
}

/* 空状态 */
.empty-content {
  text-align: center;
  padding: 20px 0;
}

.empty-text {
  font-size: 16px;
  color: #666;
  margin-bottom: 8px;
}

.empty-desc {
  font-size: 14px;
  color: #999;
}

/* 商品网格布局 */
.goods-container {
  padding: 10px 0;
}

.goods-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  margin-bottom: 30px;
}

/* 商品卡片 */
.goods-card {
  border: 1px solid #f0f0f0;
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

/* .goods-card:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
  border-color: #409eff;
} */

/* 商品图片 */
.goods-image {
  position: relative;
  width: 100%;
  height: 240px;
  overflow: hidden;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  padding: 20px;
}

.image-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.product-image {
  width: 100%;
  height: 100%;
  max-width: 180px;
  max-height: 180px;
  object-fit: contain;
  transition: all 0.3s ease;
}

.goods-card:hover .product-image {
  transform: scale(1.08);
}

/* 图片错误和加载状态 */
.image-error,
.image-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f8f9fa;
  color: #999;
  font-size: 14px;
}

.image-error i,
.image-placeholder i {
  font-size: 32px;
  margin-bottom: 8px;
  color: #ccc;
}

/* 图片遮罩层 */
.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(20, 19, 19, 0.264);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
}

.goods-card:hover .image-overlay {
  opacity: 1;
}

.overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: white;
  transform: translateY(20px);
  transition: all 0.3s ease;
}

.goods-card:hover .overlay-content {
  transform: translateY(0);
}

.overlay-content i {
  font-size: 32px;
  margin-bottom: 8px;
}

.overlay-content span {
  font-size: 14px;
  font-weight: 500;
}

/* 商品标签 */
.goods-tag {
  position: absolute;
  top: 12px;
  left: 12px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  color: white;
  z-index: 2;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.goods-tag.on-sale {
  background: linear-gradient(135deg, #ff4444 0%, #ff6b6b 100%);
}

.goods-tag.off-shelf {
  background: linear-gradient(135deg, #999 0%, #bbb 100%);
}

.brand-tag {
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 6px 12px;
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  color: white;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  z-index: 2;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* 商品信息 */
.goods-info {
  padding: 20px;
  background: #fff;
}

.goods-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2; /* 添加标准属性 */
  -webkit-box-orient: vertical;
}

.goods-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 16px;
  line-height: 1.5;
  height: 42px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2; /* 添加标准属性 */
  -webkit-box-orient: vertical;
}

/* 价格区域 */
.price-section {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.current-price {
  display: flex;
  align-items: baseline;
  color: #ff4444;
}

.price-symbol {
  font-size: 16px;
  font-weight: 600;
}

.price-number {
  font-size: 24px;
  font-weight: 700;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.save-tag {
  padding: 4px 8px;
  background: #fff2f0;
  color: #ff4444;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid #ffccc7;
}

/* 商品特性 */
.goods-features {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.feature-item {
  padding: 4px 8px;
  background: #f0f7ff;
  color: #409eff;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
  border: 1px solid #e6f3ff;
}

/* 加载更多 */
.load-more {
  text-align: center;
  padding: 40px 0;
}

.load-btn {
  padding: 12px 40px;
  font-size: 14px;
  border-radius: 20px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-container {
    padding: 15px;
  }
  
  .page-header {
    padding: 15px 0;
    margin-bottom: 20px;
  }
  
  .header-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .search-title {
    font-size: 20px;
  }
  
  .goods-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 16px;
  }
  
  .goods-image {
    height: 200px;
    padding: 15px;
  }
  
  .product-image {
    max-width: 150px;
    max-height: 150px;
  }
  
  .goods-info {
    padding: 16px;
  }
  
  .goods-name {
    font-size: 15px;
    height: 40px;
  }
  
  .goods-desc {
    font-size: 13px;
    height: 36px;
  }
  
  .price-number {
    font-size: 20px;
  }
}

@media (max-width: 480px) {
  .goods-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .goods-image {
    height: 180px;
    padding: 12px;
  }
  
  .product-image {
    max-width: 140px;
    max-height: 140px;
  }
}
</style>